package com.dg11185.nearshop.activity.user;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.nearshop.R;
import com.dg11185.libs.network.NetClient;
import com.dg11185.libs.network.http.client.HttpRequest.ActionListener;
import com.dg11185.nearshop.activity.SubPageActivity;
import com.dg11185.nearshop.app.Constants;
import com.dg11185.nearshop.app.Utils;
import com.dg11185.nearshop.net.request.AddCardRequest;
import com.dg11185.nearshop.net.response.AddCardResponse;
import com.wlper.lib.uitls.LogUtils;
import com.wlper.lib.uitls.ViewUtils;

/**
 * 添加我的会员卡
 * 
 * @author LWL_Private
 * @date 2014-05-24
 */
public class AddCardActivity extends SubPageActivity {

    private String mLoginName;

    private EditText loginNameBox, cardNoBox, passwdBox;
    private Button bindBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setSubPageTitle("绑定常惠卡");
        setContentView(R.layout.activity_add_card);
        mLoginName = getIntent().getStringExtra("loginName");
        initView();
    }

    private void findViewsId() {
        loginNameBox = (EditText) findViewById(R.id.edit_login_name);
        cardNoBox = (EditText) findViewById(R.id.edit_card_num);
        passwdBox = (EditText) findViewById(R.id.edit_passwd);
        bindBtn = (Button) findViewById(R.id.btn_bind_card);
        bindBtn.setOnClickListener(clickListener);
    }

    private void initView() {
        findViewsId();
        loginNameBox.setText(mLoginName);
        bindBtn.setOnClickListener(clickListener);
    }

    private View.OnClickListener clickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_bind_card:
                    onBindCard();
                    break;

                default:
                    break;
            }

        }
    };

    /**
     * 添加我的会员卡
     */
    private void onBindCard() {
        String cardNo = cardNoBox.getText().toString();
        String passwd = passwdBox.getText().toString();
        if (Utils.isStrNull(cardNo)) {
            ViewUtils.showToast(mActivity, "请输入卡号");
            return;
        }
        if (Utils.isStrNull(passwd)) {
            ViewUtils.showToast(mActivity, "请输入卡片密码");
            return;
        }
        showLoading();
        AddCardRequest req = new AddCardRequest(mLoginName, cardNo, passwd);
        req.setActionListener(new ActionListener<AddCardResponse>() {
            @Override
            public void onSuccess(AddCardResponse result) {
                LogUtils.logNetReturn(Constants.add, result.responseStr);
                hideLoading();
                onBindCardRet(result);
            }

            @Override
            public void onFailure(int resultCode) {
                hideLoading();
                LogUtils.logNetError("绑定失败---" + resultCode);
                if (resultCode == NetClient.NET_DISCONNECT_CODE) {
                    ViewUtils.showToast(mActivity,
                            R.string.cant_connect_network);
                } else {
                    ViewUtils.showToast(mActivity, R.string.network_error);
                }
            }
        });
        NetClient.httpPost(req);
    }

    /**
     * 处理《添加我的会员卡》的返回数据
     * 
     * @param result
     */
    private void onBindCardRet(AddCardResponse result) {
        String status = result.status;
        if (status.equals(AddCardResponse.SUCCESS)) {
            showDialog("绑定成功", "请点击“确定”返回个人中心").setPositiveButton("确定",
                    new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            hideDialog();
                            setResult(RESULT_OK);
                            mActivity.finish();
                        }
                    });
        } else if (status.equals(AddCardResponse.CARD_NOTFOUND)) {
            ViewUtils.showToast(mActivity, "卡号不存在");
        } else if (status.equals(AddCardResponse.CARD_ILLEGALPWD)) {
            ViewUtils.showToast(mActivity, "卡密错误");
        } else if (status.equals(AddCardResponse.CARD_MAPPING)) {
            ViewUtils.showToast(mActivity, "该卡已被绑定");
        } else if (status.equals(AddCardResponse.CARD_INVALID)) {
            ViewUtils.showToast(mActivity, "该卡已锁定");
        }else {
            LogUtils.logNetError("绑定失败---"+status);
            ViewUtils.showToast(mActivity, R.string.network_error);
        }
    }
}
